package org.goyo.main.web.controller.system;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.goyo.common.baseBean.AjaxResult;
import org.goyo.common.utils.JSONUtil;
import org.goyo.main.domain.entity.system.SysMenu;
import org.goyo.main.services.system.ISysMenuService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 系统资源
 * 
 * @author chenchuan
 * 2014年10月23日
 */
@Controller
@RequestMapping("/sysMenu")
public class SysMenuController {
	protected Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private ISysMenuService sysMenuService;
	/**
	 * 资源list
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/toSysMenuList")
	public String toSysMenuList(HttpServletRequest request,HttpServletResponse response,Model model){
		List<SysMenu> list = null;
		try {
			list = sysMenuService.getAllSysMenu();
			model.addAttribute("list", JSONUtil.obj2Json(list));
		} catch (Exception e) {
			logger.error(e.getMessage(),e);
		}
		return "/system/sysMenuList";
	}
	
	/**
	 * 资源保存
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/saveOrUpdateSysMenu")
	@ResponseBody
	public AjaxResult saveOrUpdateSysMenu( SysMenu sysMenu,HttpServletRequest request,HttpServletResponse response){
		
		AjaxResult result = null;
		try {
			String uuid = sysMenuService.saveOrUpdateInfo(sysMenu);
			List<SysMenu> list = sysMenuService.getAllSysMenu();
			result = AjaxResult.resultSuccess("保存成功", list,uuid);
		} catch (Exception e) {
			result = AjaxResult.resultFail("保存失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	/**
	 * 资源删除
	 * 
	 * @author chenchuan
	 * 2014年10月23日
	 */
	@RequestMapping("/delSysMenu")
	@ResponseBody
	public AjaxResult delSysMenu(HttpServletRequest request){
		AjaxResult result = null;
		try {
			String uuid = request.getParameter("uuid");
			sysMenuService.delByUuids(new String[]{uuid});
			List<SysMenu> list = sysMenuService.getAllSysMenu();
			result = AjaxResult.resultSuccess("删除成功", list);
		} catch (Exception e) {
			result = AjaxResult.resultFail("删除失败", null);
			logger.error(e.getMessage(),e);
		}
		return result;
	}
	
}
